<?php
include "DBconf.php";
include "Redirect.php";

session_start();
if(isset($_SESSION['uid']))
{
    redirect("index.php");
}

if (isset($_POST['username'])) {

    $status = array();
    $status['error'] = false;
    $status['message'] = '';
    $username = $_POST['username'];
    $pass = $_POST['password'];
    $name = $_POST['name'];
    $data_nasc = $_POST['birth'];
    $email = $_POST['email'];


    if ($username == '' || $pass == '' || $name == '' || $data_nasc == '' || $email == '') {
        $status['message'] = 'Existem campos vazios.';
        $status['error'] = true;
    } else {

        $query = "INSERT INTO usuario (login,senha,nome,dat_nasc,email)
                VALUES('$username','$pass','$name',str_to_date('$data_nasc','%d/%m/%Y'),'$email')";

        $connection = mysql_connect($server, $db_user, $db_pass);

        if (!$connection) {
            $status['message'] = 'Erro na conexão com o BD.';
            $status['error'] = true;
        } else if (mysql_select_db($db_name, $connection)) {
            $result = mysql_query($query);

            if (!$result) {
                $status['message'] = mysql_error($connection);
                $status['error'] = true;
            } else {
                $status['message'] = 'Usuário cadastrado.';
                $status['error'] = false;
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Kiwitter</title>
        <link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
        <link type="text/css" rel="stylesheet" href="css/datepicker.css"/>
        <link rel="shortcut icon" href="img/kiwicon.ico">
    </head>
    <body>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="brand" href="index.php">Kiwitter</a>
                    <ul class="nav">
                        <li class=""><a href="index.php">Inicio</a></li>
                        <li class="active"><a href="#">Cadastro</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="container">
            <?php
            if (isset($status['error'])) {
                if ($status['error']) {
                    ?>
                    <div class="alert alert-error">
                        <button class="close" data-dismiss="alert">×</button>
                        <strong>Oops!</strong> <?php echo $status['message']; ?>
                    </div>
                <?php } else { ?>
                    <div class="alert alert-success">
                        <button class="close" data-dismiss="alert">×</button>
                        <i class="icon-ok"></i>&nbsp;Os seus dados foram cadastrados!&nbsp;<a href="index.php?login=true">Clique aqui para efetuar o login</a>
                    </div>
                    <?php
                }
            }
            ?>
            <div id="error-alert"></div>
            <div class="row">
                <div class="offset2 span8">
                    <form class="form-horizontal" id="cadastro-form" method="POST" 
                          action="" onsubmit="return valida_form('cadastro-form')">
                        <fieldset>
                            <legend>Dados</legend>
                            <div class="control-group" id="cad-login">
                                <label class="control-label" for="input1"><p>Login</p></label>
                                <div class="controls">
                                    <input class="input-large" id="input1" type="text" name="username"
                                           onblur="loginValidator.checkLoginName()"/>
                                    <span id="login-help" class="help-inline"></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="input2"><p>Senha</p></label>
                                <div class="controls">
                                    <input class="input-large" id ="input2" type="password" name="password"/>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="input3"><p>Nome</p></label>
                                <div class="controls">
                                    <input class="input-large" id="input3" type="text" name="name"/>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="input4"><p>Aniversário</p></label>
                                <div class="controls">
                                    <input class="input-large" id="input4" type="text" name="birth"
                                           data-date-format="dd/mm/yyyy" value="01/01/1991"/>
                                </div>
                            </div>
                            <div class="control-group" id="cad-email">
                                <label class="control-label" for="input5"><p>E-mail</p></label>
                                <div class="controls">
                                    <input class="input-large" id="input5" type="text" name="email"
                                           onkeyup="valida_email('cad-email')"
                                           onchange="valida_email('cad-email')"/>
                                    <span class="help-inline"></span>
                                </div>
                            </div>
                            <div class="form-actions">
                                <div class="row">
                                    <div class="offset1">
                                        <input type="submit" class="btn btn-primary btn-large" value="Cadastrar">
                            </div>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
        <script type="text/javascript" src="js/jQuery-1.7.1-min.js"></script>
        <script type="text/javascript" src="js/bootstrap-2.0.3.js"></script>
        <script type="text/javascript" src="js/bootstrap-datepicker.js"></script>
        <script type="text/javascript">
            $('#input4').datepicker();
        </script>
        <script type="text/javascript" src="js/validacao.js"></script>
        <script type="text/javascript" src="js/does-user-exist.js"></script>
    </body>
</html>